<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="classifyName">
              <el-input v-model="filters.classifyName" placeholder="分类名称" clearable></el-input>
            </el-form-item>
            <el-form-item prop="businessId">
              <el-select v-model="filters.businessId" placeholder="商户" clearable filterable>
                <el-option v-for="item in businessList" :value="item.id" :label="item.name" :key="item.id"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
            <el-button type="primary" plain icon="fa fa-plus" v-has="permissionBatchDelayNotice" @click="handleClickSet('', 2)" size="mini"> 批量设置延迟通知</el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table
        @selection-change="handleSelectionChange"
        tooltip-effect="dark"
        ref="tableList"
        :data="dataList"
        :fit="true"
        highlight-current-row
        v-loading="listLoading"
        element-loading-text="数据加载中，请稍后..."
        empty-text="对不起，没有找到相关数据"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column prop="classifyName" align="center" label="分类名称">
          <template slot-scope="scope">
            <div class="business-classify_name" v-html="scope.row.classifyName"></div>
          </template>
        </el-table-column>
        <el-table-column prop="classifyId" align="center" label="分类ID"> </el-table-column>
        <el-table-column prop="businessName" align="center" label="商户"> </el-table-column>
        <!-- <el-table-column prop="officeName" align="center" label="关联科室"></el-table-column> -->
        <el-table-column prop="sort" align="center" label="排序（倒序）"> </el-table-column>
        <el-table-column prop="createDate" align="center" label="创建时间"> </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
                <el-dropdown-item v-has="permissionEditDelayNotice" @click.native="handleClickSet(scope.row, 1)">编辑延时通知</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
    <delayNotice :delayType="1" :show="delayNoticeShow" :ids="multipleSelection" :operatorType="operatorType" @delayNoticeClose="delayNoticeClose" @delayNoticeConfirm="delayNoticeConfirm" />
  </div>
</template>

<script>
import delayNotice from '@/components/DelayNotice';
import * as message from '@/constants/message';
import BusinessClassifyApi from '@/api/business/businessClassify';
import BusinessApi from '@/api/business/business';
export default {
  name: 'BusinessClassifyManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      delayNoticeShow: false,
      filters: {
        classifyName: '',
        businessId: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      businessList: [],
      // 权限

      permissionAdd: 'SYSTEM_MANAGER_BUSINESS_CLASSIFY_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_BUSINESS_CLASSIFY_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_BUSINESS_CLASSIFY_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_BUSINESS_CLASSIFY_LIST_DELETE',
      permissionUpStatus: 'SYSTEM_MANAGER_BUSINESS_CLASSIFY_LIST_STATE',
      permissionBatchDelayNotice: 'SYSTEM_MANAGER_BUSINESS_CLASSIFY_LIST_BATCH_DELAY_NOTICE',
      permissionEditDelayNotice: 'SYSTEM_MANAGER_BUSINESS_CLASSIFY_LIST_EDIT_DELAY_NOTICE',
      multipleSelection: [],
      operatorType: 1,
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  components: {
    delayNotice,
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val.map((obj) => obj.classifyId);
    },
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listData();
      _this.getBusinessList();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    getBusinessList() {
      BusinessApi.listIdNameVo().then((res) => {
        this.businessList = res.data;
      });
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listData();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listData();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      var cond = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        name: this.filters.classifyName,
        businessId: this.filters.businessId,
      };
      // 发送请求
      BusinessClassifyApi.listBusinessClassify(cond)
        .then((res) => {
          this.listLoading = false;
          this.dataList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.$refs.tableList.doLayout();
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },
    formatState(cellValue) {
      if (cellValue === -1) {
        return '删除';
      } else if (cellValue === 0) {
        return '禁用';
      } else if (cellValue === 1) {
        return '启用';
      }
    },
    formatStates(cellValue) {
      if (cellValue === -1) {
        return 'danger';
      } else if (cellValue === 0) {
        return 'info';
      } else if (cellValue === 1) {
        return 'success';
      }
    },
    handleClickDelete(val) {
      // 删除
      this.$confirm('是否删除该模板', message.TITLE_PROMPT, {}).then(() => {
        var cond = {
          id: val.classifyId,
          state: -1,
        };
        BusinessClassifyApi.deleteBusinessClassify(cond).then((data) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/business/businessClassifyDetail',
        query: { id: row.classifyId },
      });
    },
    handleClickEditor(val) {
      //编辑 进入新增页通过id来查找数据
      let _this = this;
      _this.$router.push({
        path: '/business/businessClassifyEdit',
        query: { id: val.classifyId },
      });
    },
    handleClickAdd() {
      // 刷新
      let _this = this;
      _this.$router.push('/business/businessClassifyAdd');
    },
    handleClickSet(item, s) {
      //s 1等于编辑 2等于批量设置
      this.operatorType = s;
      if (item.classifyId) {
        for (let i in this.dataList) {
          this.$refs.tableList.toggleRowSelection(this.dataList[i], false);
        }
        this.multipleSelection = [item.classifyId];
        this.$refs.tableList.toggleRowSelection(item, true);
      }
      if (this.multipleSelection.length == 0) {
        this.$message.error('至少选择一项分类');
        return false;
      }
      this.delayNoticeShow = true;
    },
    delayNoticeClose() {
      this.delayNoticeShow = false;
    },
    delayNoticeConfirm() {
      this.delayNoticeShow = false;
      this.listData();
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
<style lang="scss" scoped>
.img-wrapper {
  width: 80px;
  height: 60px;
  overflow: hidden;
  border-radius: 3px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin: 0 auto;

  .el-image {
    width: 100%;

    &::v-deep img {
      width: 100%;
      height: auto;
    }
  }
}

.business-classify_name {
  ::v-deep p {
    margin: 0;

    img {
      vertical-align: middle;
      height: 16px;
      width: auto;
    }
  }
}
</style>
